#!/usr/bin/perl

do '../web-lib.pl';

&init_config();

#get_pdcuser_list()
#return @userlist;
sub get_pdcuser_list()
{
	my (@userlist, $i);
	#system("$config{'winbind_info_user'} >$config{'pdcuserfile'}");
	open(PDCUSER, $config{"pdcuserfile"});
	@userlist = <PDCUSER>;
	close(PDCUSER);

	$i = 0;
	while($i < @userlist)
	{
		chomp($userlist[$i]);
		$userlist[$i] = lc($userlist[$i]);
		$i++;
	}

	sort @userlist;
	return @userlist;
}

sub get_pdcgroup_list()
{
	my (@grouplist, $i);
	#system("$config{'winbind_info_group'} >$config{'pdcgroupfile'}");
	open(PDCGROUP, $config{"pdcgroupfile"});
	@grouplist = <PDCGROUP>;
	close(PDCUSER);

	$i = 0;
	while($i < @grouplist)
	{
		chomp($grouplist[$i]);
		$grouplist[$i] = lc($grouplist[$i]);
		$i++;
	}

	@grouplist = sort @grouplist;
	return @grouplist;
}

sub winbind_restart()
{
	#`$config{'winbind_restart'}`;
	my $out, $i;

#	$out = `$config{'winbind_stop'}`;

#	$out = `$config{'winbind_status'}`;

#	$i = 0;

#	while(($out =~ /stopped/) && ($i < 2))
#	{
#		$out = `$config{'winbind_start'}`;
#		sleep(2);
#		$out = `$config{'winbind_status'}`;
		#print "<p>", $out;
#		$i++;
#	}
	$out = `$config{'winbind_restart'} 2>/dev/null`;
}

sub winbind_is_ping()
{
	my @out, $result;
	@out = `$config{'winbind_ping'} 2>/dev/null`;
	$result = join(" ", @out);
	if($result =~ /succeed/)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

#checkpdcfile(void)
#return $error_msg
sub checkpdcfile()
{
	my (@pdcuser, @pdcgroup, @line, $i, $reset, $error_msg);

	$reset = "no";
	open(PDCUSER, $config{"pdcuserfile"});
	@pdcuser = <PDCUSER>;
	close(PDCUSER);

	if(scalar(@pdcuser) < 2)
	{
		for($i = 0; $i < @pdcuser; $i++)
		{
			@line = split(" ", $pdcuser[$i]);
			if(scalar(@line) >= 2)
			{
				$reset = "yes";
				$error_msg = $text{'error_msg_passwd_err'};
				last;
			}
		}
	}
	if($reset eq "yes")
	{
		open(PDCUSER, ">$config{'pdcuserfile'}");
		close(PDCUSER);
	}

	$reset = "no";
	open(PDCGROUP, $config{"pdcgroupfile"});
	@pdcgroup = <PDCGROUP>;
	close(PDCGROUP);

	if(scalar(@pdcgroup) < 2)
	{
		for($i = 0; $i < @pdcgroup; $i++)
		{
			@line = split(" ", $pdcgroup[$i]);
			if(scalar(@line) >= 2)
			{
				$reset = "yes";
				$error_msg = $text{'error_msg_passwd_err'};
				last;
			}
		}
	}
	if($reset eq "yes")
	{
		open(PDCGROUP, ">$config{'pdcgroupfile'}");
		close(PDCGROUP);
	}
	return $error_msg;
}

#getent_passwd_fullname(\%fullname)
sub getent_passwd_fullname()
{
	my ($fullname, @entpasswd, $i, @line);
	$fullname = @_[0];

	open(GETENTPASSWD, "$config{'command_getent_passwd'}|");
	@entpasswd = <GETENTPASSWD>;
	close(GETENTPASSWD);

	for($i = 0; $i < @entpasswd; $i++)
	{
		$entpasswd[$i] =~ s/#.*$//;
		@line = split(":", $entpasswd[$i]);
		if($line[0])
		{
			$line[0] = lc($line[0]);
			$fullname->{$line[0]} = $line[4];
		}
	}
}
#getent_group_members(\%members)
sub getent_group_members()
{
	my ($members, @entgroup, $i, @line, @memberline);
	$members = @_[0];

	open(GETENTGROUP, "$config{'command_getent_group'}|");
	@entgroup = <GETENTGROUP>;
	close(GETENTGROUP);

	for($i = 0; $i < @entgroup; $i++)
	{
		$entgroup[$i] =~ s/#.*$//;
		@line = split(":", $entgroup[$i]);
		if($line[0])
		{
			$line[0] = lc($line[0]);
   			#$line[3] =~ s/,/, /g;
			$line[3] = lc($line[3]);
			@memberline = split(",", $line[3]);
			@memberline = sort @memberline;
			$line[3] = join(", ", @memberline);
			$members->{$line[0]} = $line[3];
		}
	}
}

1;
